package com.matrix.gui;

import java.io.File;
import com.matrix.utils.FileIO;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Creates a file browser for the downloads folder where the user can load
 * a matrix, column vector, vector, constant, or an entire session from a text file
 * @author McGarvie
 *
 */
public class LoadMatrix extends ListActivity {
    /** Called when the activity is first created. */
    static String[] FILES = null;
    Globals gvars;
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	boolean mExternalStorageAvailable = false;
 		boolean mExternalStorageWriteable = false;
 		String state = Environment.getExternalStorageState();
 		gvars = (Globals) this.getApplicationContext();
 		if (Environment.MEDIA_MOUNTED.equals(state)) 
 		{    
 			// We can read and write the media    
 			mExternalStorageAvailable = mExternalStorageWriteable = true;
 		} 
 		else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
 		{    
 			// We can only read the media    
 			mExternalStorageAvailable = true;    
 			mExternalStorageWriteable = false;	
 		} 
 		else 
 		{    
 			// Something else is wrong. It may be one of many other states, but all we need    
 			//  to know is we can neither read nor write    
 			mExternalStorageAvailable = mExternalStorageWriteable = false;
 		}
 		File[] filelist = null;
 		int count = 0;
 		if(mExternalStorageAvailable)
 		{
 			File root = Environment.getExternalStorageDirectory();
			File f = new File(root.getAbsolutePath()+"/download/");
			filelist = f.listFiles();
			for(int i=0; i<filelist.length; i++)
			{
				if(filelist[i].getName().endsWith(".txt"))
				{
					count++;
				}
			}
 		}
 		if(filelist == null)
 		{
 			FILES = new String[count];
 		}
 		else
 		{
 			FILES = new String[count];
 			int j = 0;
 			for(int i=0; i< filelist.length; i++)
 			{
 				if(filelist[i].getName().endsWith(".txt"))
 				{
 					FILES[j] = filelist[i].getName();
 					j++;
 				}
 			}
 		}
 		
    	setListAdapter(new ArrayAdapter<String>(this, R.layout.file_row, FILES));
    	
    	ListView lv = getListView();
    	lv.setBackgroundColor(Color.WHITE);
    	
    	lv.setTextFilterEnabled(true);

    	lv.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			FileIO f = new FileIO();
			f.loadMatrix(((TextView)arg1).getText().toString(), gvars.getListItems());
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
			
		}
    	});
    }
}